const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const Shop = sequelize.define('Shop', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    address: {
      type: DataTypes.STRING(255),
      allowNull: false
    },
    phone: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    business_hours: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    delivery_range: {
      type: DataTypes.DECIMAL(5, 2),
      defaultValue: 3,
      comment: '配送范围(公里)'
    },
    delivery_fee: {
      type: DataTypes.DECIMAL(10, 2),
      defaultValue: 5,
      comment: '配送费'
    },
    min_delivery_amount: {
      type: DataTypes.DECIMAL(10, 2),
      defaultValue: 20,
      comment: '最低配送金额'
    },
    logo: {
      type: DataTypes.STRING(255)
    },
    latitude: {
      type: DataTypes.DECIMAL(10, 6),
      allowNull: false,
      comment: '店铺纬度'
    },
    longitude: {
      type: DataTypes.DECIMAL(10, 6),
      allowNull: false,
      comment: '店铺经度'
    },
    rating: {
      type: DataTypes.DECIMAL(2, 1),
      defaultValue: 0,
      comment: '店铺评分(0-5分)'
    },
    rating_count: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '评分人数'
    },
    is_open: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      comment: '是否营业'
    },
    created_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      onUpdate: DataTypes.NOW
    }
  }, {
    tableName: 'shop_info',
    timestamps: false,
    indexes: [
      { name: 'idx_shop_location', fields: ['latitude', 'longitude'] },
      { name: 'idx_shop_rating', fields: [ { attribute: 'rating', order: 'DESC' } ] }
    ]
  });

  return Shop;
};
